using System;
using System.Configuration;

namespace NHConfiguration
{
    public class EndPointCollection : ConfigurationElementCollection
    {
        public EndPointElement this[int index]
        {
            get
            {
                return BaseGet(index) as EndPointElement;
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new EndPointElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((EndPointElement)element).Contract;
        }

        public EndPointElement GetElementByKey(string contractName)
        {
            foreach (EndPointElement service in this)
            {
                if (string.Equals(contractName, service.Contract, StringComparison.InvariantCultureIgnoreCase))
                    return service;
            }

            return null;
        }
    }
}